//
//  SRClassHomeworkListTableViewCell.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/7/20.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRClassHomeworkListTableViewCell.h"
#import <NSDate+DateToDisplayString.h>
#import "UIButton+FZGreenButton.h"
#import "SRClassConstants.h"

NSString *const kSRClassHomeworkListTableViewCell = @"SRClassHomeworkListTableViewCell";


@interface SRClassHomeworkListTableViewCell ()

@property (weak, nonatomic) IBOutlet UIView *borderView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *lessonLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *scoreImageView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIView *coverView;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
@property (weak, nonatomic) IBOutlet UIButton *doHomeworkButton;

@property (strong, nonatomic) SRWorkListItemModel *model;

@end

@implementation SRClassHomeworkListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
        // Initialization code
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    
    self.lessonLabel.textColor = css.color_1;
    self.lessonLabel.font = css.fontOfH3;
    
    self.descLabel.textColor = css.color_1;
    self.descLabel.font = css.fontOfH5;
    
    self.timeLabel.textColor = css.color_1;
    self.timeLabel.font = css.fontOfH7;
    
    self.scoreLabel.textColor = css.color_4;
    self.scoreLabel.font = css.fontOfH5;
    
    self.borderView.clipsToBounds = YES;
    self.borderView.layer.cornerRadius = 5;
    self.borderView.backgroundColor = css.color_5;
    
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.coverView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    self.coverView.clipsToBounds = YES;
    self.coverView.layer.cornerRadius = 5;
//    self.coverView.backgroundColor = css.color_5;

    [self.doHomeworkButton applyGreenBackgroundColor];
    self.doHomeworkButton.titleLabel.font = css.fontOfH8;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
}

- (void)updateCellInfo:(SRWorkListItemModel *)model {
    if (!model) {
        return;
    }
    
    self.model = model;
    self.lessonLabel.text = model.title;
    self.descLabel.text = model.unit;
    [self.iconImageView fz_setImageWithDefaultPlaceholderWithURL:[NSURL URLWithString:model.page_url] placeholderSize:CGSizeZero];
    
    
    if (self.userType == SRSchoolClassUserTypeTeacher) {
        self.doHomeworkButton.hidden = NO;
        self.scoreImageView.hidden = YES;
        self.scoreLabel.hidden = YES;
        
        NSInteger curNum = MAX(0, [self.model.cur_num integerValue]);
        NSInteger limitNum = MAX(curNum, [self.model.limit_num integerValue]);
        self.timeLabel.text = [NSString stringWithFormat:@"完成度：%lu/%lu", (unsigned long)curNum, limitNum];

        if (self.model.ctype == SRClassHomeworkTypeDubbing) {
            [self.doHomeworkButton setTitle:@"配音题" forState:UIControlStateNormal];
        } else if (self.model.ctype == SRClassHomeworkTypePicture) {
            [self.doHomeworkButton setTitle:@"选图答题" forState:UIControlStateNormal];
        } else if (self.model.ctype == SRClassHomeworkTypeRecord) {
            [self.doHomeworkButton setTitle:@"课堂录音" forState:UIControlStateNormal];
        } else if (self.model.ctype == SRClassHomeworkTypeAudio) {
            [self.doHomeworkButton setTitle:@"语音答题" forState:UIControlStateNormal];
        }
        
    } else {
        
        self.doHomeworkButton.hidden = NO;
        self.scoreImageView.hidden = YES;
        self.scoreLabel.hidden = YES;
        
        self.doHomeworkButton.userInteractionEnabled = YES;
        self.timeLabel.text = [NSDate dateToDetailDisplayStringForTimeInterval:[model.create_time longLongValue]];
      
        if (self.model.ctype == SRClassHomeworkTypeDubbing) {
            [self.doHomeworkButton setTitle:@"配音题" forState:UIControlStateNormal];
//            [self.doHomeworkButton setTitle:@"去完成" forState:UIControlStateNormal];
            
            if ([model.finishArray count] > 0) {
                    //已完成
                    //            self.doHomeworkButton.hidden = YES;
                    //            self.doHomeworkButton.userInteractionEnabled = YES;
                SRWorkListItemModel *showModel = [model.finishArray firstObject];
                
                if (showModel.comment && [showModel.comment length] > 0) {
                    [self.doHomeworkButton setTitle:@"看点评" forState:UIControlStateNormal];
                } else {
                    [self.doHomeworkButton setTitle:@"已完成" forState:UIControlStateNormal];
                }
                
                NSInteger score = [showModel.score integerValue];
                if (score > 0) {
                    self.scoreImageView.hidden = NO;
                    self.scoreLabel.hidden = NO;
                    self.scoreLabel.text = [NSString stringWithFormat:@"%ld", (long)score];
                    if (score > 60) {
                        self.scoreImageView.image = [UIImage imageNamed:@"pass"];
                    } else {
                        self.scoreImageView.image = [UIImage imageNamed:@"unpass"];
                    }
                } else {
                    self.scoreImageView.hidden = YES;
                    self.scoreLabel.hidden = YES;
                }
            } else {
                self.scoreImageView.hidden = YES;
                self.scoreLabel.hidden = YES;
            }

        } else if (self.model.ctype == SRClassHomeworkTypePicture) {
            [self.doHomeworkButton setTitle:@"选图答题" forState:UIControlStateNormal];
            if ([model.finishArray count] > 0) {
                self.doHomeworkButton.userInteractionEnabled = NO;
                [self.doHomeworkButton setTitle:@"已完成" forState:UIControlStateNormal];
            }
        } else if (self.model.ctype == SRClassHomeworkTypeRecord) {
            [self.doHomeworkButton setTitle:@"课堂录音" forState:UIControlStateNormal];
//            if ([model.finishArray count] > 0) {
//                self.doHomeworkButton.userInteractionEnabled = NO;
//
//                [self.doHomeworkButton setTitle:@"已完成" forState:UIControlStateNormal];
//            }
        } else if (self.model.ctype == SRClassHomeworkTypeAudio) {
            [self.doHomeworkButton setTitle:@"语音答题" forState:UIControlStateNormal];
            if ([model.finishArray count] > 0) {
                self.doHomeworkButton.userInteractionEnabled = NO;

                [self.doHomeworkButton setTitle:@"已完成" forState:UIControlStateNormal];
            }
        }
        
        if (self.model.ctype != SRClassHomeworkTypeRecord && [model.finishArray count] > 0) {
                //已完成
            SRWorkListItemModel *showModel = [model.finishArray firstObject];
            
            if (showModel.comment && [showModel.comment length] > 0) {
                [self.doHomeworkButton setTitle:@"看点评" forState:UIControlStateNormal];
            } else {
                [self.doHomeworkButton setTitle:@"已完成" forState:UIControlStateNormal];
            }
        }
        
    }
    
}

- (void)setIsEditStatus:(BOOL)isEditStatus {
    _isEditStatus = isEditStatus;
    
    self.coverView.hidden = !isEditStatus;
    self.deleteButton.hidden = !isEditStatus;
}

- (IBAction)onDeleteButton:(id)sender {
    if (self.touchDeleteButtonBlock) {
        self.touchDeleteButtonBlock(self.model);
    }
}

- (IBAction)onDoHomeworkButton:(id)sender {
    
    if (self.touchDoHomeworkButtonBlock) {
        self.touchDoHomeworkButtonBlock(self.model) ;
    }
}


@end
